########################################################################
#  Wiizard - A Wii games manager
#  Copyright (C) 2023  CYBERDEViL
#
#  This file is part of Wiizard.
#
#  Wiizard is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Wiizard is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################


import os
import sys

from wiizard.const import (
  IMG_FRONT_COVER,
  IMG_FRONT_3D_COVER,
  IMG_DISC,
  IMG_FULL
)

class ImageCacheStructure:
  Front2d = "covers/2D/{id6}.png"
  Front3d = "covers/3D/{id6}.png"
  Full    = "covers/Full/{id6}.png"
  Disc    = "covers/Disc/{id6}.png"

  @staticmethod
  def subdirs():
    return (
      os.path.dirname(__class__.Front2d),
      os.path.dirname(__class__.Front3d),
      os.path.dirname(__class__.Full),
      os.path.dirname(__class__.Disc)
    )

  @staticmethod
  def getType(imgType):
    if (imgType & IMG_FRONT_COVER):
      return __class__.Front2d

    if (imgType & IMG_FRONT_3D_COVER):
      return __class__.Front3d

    if (imgType & IMG_FULL):
      return __class__.Full

    if (imgType & IMG_DISC):
      return __class__.Disc

  @staticmethod
  def getPath(imgType):
    return os.path.dirname(__class__.getType(imgType))

  @staticmethod
  def getFullPath(imgType, id6Str, language):
    return __class__.getType(imgType).format_map({'id6':id6Str, 'lang':language})

  @staticmethod
  def getFilename(imgType, id6Str, language):
    return os.path.basename(__class__.getType(imgType).format(id6=id6Str, lang=language))


class ImageCache:
  def __init__(self, path="./cache", structure=ImageCacheStructure):
    self.__path   = path
    self.__struct = structure

    """ Main image cache dir """
    if os.path.exists(path):
      if not os.path.isdir(path):
        print("ERROR ImageCache path exists but is not a dir:", path)
        sys.exit(1)  # TODO do not exit

    # Create image cache path
    else:
      os.makedirs(path)

    """ structure subs dirs """
    for subdir in structure.subdirs():
      realpath = os.path.join(path, subdir)
      if os.path.exists(realpath):
        if not os.path.isdir(realpath):
          print("ERROR ImageCache path exists but is not a dir:",realpath)
          sys.exit(1)  # TODO do not exit
      else:
        os.makedirs(realpath)

  @property
  def path(self):
    return self.__path

  def setPath(self, path):
    self.__path = path

  def getImagePath(self, id6Str, imgType, lang=""):
    return os.path.join(self.__path, self.__struct.getFullPath(imgType, id6Str, lang))

  def getImage(self, id6Str, imgType, lang=""):
    path = self.getImagePath(id6Str, imgType, lang)
    if os.path.exists(path):
      return path
    return None
